//
//  RegisterViewController.m
//  Kjs-iOS
//
//  Created by cfq on 16/6/4.
//  Copyright © 2016年 jmhz. All rights reserved.
//

#import "RegisterViewController.h"
#import "KJSRequest.h"
#import "LoginViewController.h"
#import "UIColor+KjsColor.h"
#import "CommonUtil.h"
#import "ProtocolViewController.h"
#import <CoreText/CoreText.h>

#define SCREEN_W [[UIScreen mainScreen] bounds].size.width //屏幕宽度
#define SCREEN_H [[UIScreen mainScreen] bounds].size.height //屏幕高度
#define NAV_BAR_H self.navigationController.navigationBar.frame.size.height
#define CodeTimeInterval 180
@interface RegisterViewController ()
@property (nonatomic,weak) IBOutlet UILabel *protocolLab;
@property (weak, nonatomic) IBOutlet UIButton *getCodeBtn;
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;
@property (weak, nonatomic) IBOutlet UIButton *acceptAgBtn;

@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *pswTF;
@property (weak, nonatomic) IBOutlet UITextField *confirmPswTF;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet UIButton *pswEyeBtn;
@property (weak, nonatomic) IBOutlet UIButton *confirmPswEyeBtn;

@property (strong,nonatomic) NSString *tempStr;
@property (assign, nonatomic) BOOL isCheck;

@property (nonatomic,assign) int secondsCountDown;//倒计时总时长
@property (nonatomic,strong) NSTimer *countDownTimer;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"注册"];
    
    self.pswTF.secureTextEntry = YES;
    self.confirmPswTF.secureTextEntry = YES;
    self.getCodeBtn.layer.cornerRadius = 10;
    self.registerBtn.layer.cornerRadius = 10;
    
    [self.acceptAgBtn addTarget:self action:@selector(acceptAgreement) forControlEvents:UIControlEventTouchUpInside];
    [self.getCodeBtn addTarget:self action:@selector(sendMessage) forControlEvents:UIControlEventTouchUpInside];
    [self.registerBtn addTarget:self action:@selector(Register) forControlEvents:UIControlEventTouchUpInside];

    self.pswEyeBtn.tag = 1;
    [self.pswEyeBtn addTarget:self action:@selector(swichPasswordVisiable:) forControlEvents:UIControlEventTouchUpInside];
    self.confirmPswEyeBtn.tag = 2;
    [self.confirmPswEyeBtn addTarget:self action:@selector(swichPasswordVisiable:) forControlEvents:UIControlEventTouchUpInside];
    
    UITapGestureRecognizer *goRegisterPro = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goProtocolViewController)];
    self.protocolLab.userInteractionEnabled = YES;
    [self.protocolLab addGestureRecognizer:goRegisterPro];
    //添加下划线
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:self.protocolLab.text];
    [attString addAttribute:(NSString*)kCTUnderlineStyleAttributeName value:[NSNumber numberWithInt:kCTUnderlineStyleSingle] range:(NSRange){0,[attString length]}];
    self.protocolLab.attributedText = attString;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.isCheck = NO;
    self.registerBtn.enabled = NO;
    [self.registerBtn setBackgroundColor:[UIColor lightGrayColor]];
    self.secondsCountDown = CodeTimeInterval;
    [self.acceptAgBtn setBackgroundImage:[UIImage imageNamed:@"checkbox-unchecked"] forState:UIControlStateNormal];

}
#pragma mark - custom method
-(void)goProtocolViewController
{
    ProtocolViewController *protocolVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"ProtocolViewController"];
    protocolVC.proType = @"8";
    protocolVC.proTitle = self.protocolLab.text;
    [self displayViewController:protocolVC];
}
-(void)swichPasswordVisiable:(id)sender
{
    UIButton *eyeBtn = (UIButton *)sender;
    switch (eyeBtn.tag) {
        case 1:
        {
            self.tempStr = self.pswTF.text;
            self.pswTF.text = @"";
            self.pswTF.secureTextEntry = !self.pswTF.secureTextEntry;
            self.pswTF.text = self.tempStr;
        }
            break;
        case 2:
        {
            self.tempStr = self.confirmPswTF.text;
            self.confirmPswTF.text = @"";
            self.confirmPswTF.secureTextEntry = !self.confirmPswTF.secureTextEntry;
            self.confirmPswTF.text = self.tempStr;
        }
            break;
        default:
            break;
    }
   
}
-(void)acceptAgreement
{
    if (self.isCheck == YES){
        self.isCheck = NO;
        [self.acceptAgBtn setBackgroundImage:[UIImage imageNamed:@"checkbox-unchecked"] forState:UIControlStateNormal];
    }else if (_isCheck == NO){
        self.isCheck = YES;
        [self.acceptAgBtn setBackgroundImage:[UIImage imageNamed:@"checkbox-checked"] forState:UIControlStateNormal];
    }
}

-(void)Register
{
    if ([self.phoneTF.text isEqualToString:@""]) {
        [GUATool showDialog:@"请输入手机号" message:nil waitingTime:1.5];
    }else if (![CommonUtil validateMobile:self.phoneTF.text]){
        [GUATool showDialog:@"请输入正确的手机号" message:nil waitingTime:1.5];
    }else if ([self.pswTF.text isEqualToString:@""]){
        [GUATool showDialog:@"请输入密码" message:nil waitingTime:1.5];
    }else if (![CommonUtil validatePassword:self.pswTF.text]) {
        [GUATool showDialog:@"密码必须是6-16位的数字与字母的组合" message:nil waitingTime:1.5];
    }else if ([self.confirmPswTF.text isEqualToString:@""]){
        [GUATool showDialog:@"请再次输入密码" message:nil waitingTime:1.5];
    }else if (![self.pswTF.text isEqualToString:self.confirmPswTF.text]){
        [GUATool showDialog:@"两次输入的密码不一致" message:nil waitingTime:1.5];
    }else if ([self.codeTF.text isEqualToString:@""]){
        [GUATool showDialog:@"请输入验证码" message:nil waitingTime:1.5];
    }else if (self.isCheck == NO){
        [GUATool showDialog:@"请勾选阅读协议" message:nil waitingTime:1.5];
    }else{
        [KJSRequest RegisterAcceptMessageWithPhoneNumber:self.phoneTF.text userLoginId:self.phoneTF.text password:self.pswTF.text verificationCode:self.codeTF.text ret:^(RegisterModel *model) {
            if (model.IOSUserLogin) {
                if ([model.IOSUserLogin.partyId isEqualToString:@""]){
                    [GUATool showDialog:@"注册失败" message:nil waitingTime:1.5];
                }else{
                    self.successRegister(self.phoneTF.text,self.pswTF.text);
                    [GUATool showDialog:@"注册成功" message:nil waitingTime:1.5];
                    [self dismiss:YES];
                }
            }
        }];
    }
}
-(void)sendMessage
{
    
    if ([self.phoneTF.text isEqualToString:@""]) {
        [GUATool showDialog:@"请输入手机号" message:nil waitingTime:1.5];
    }else if (![CommonUtil validateMobile:self.phoneTF.text]) {
        [GUATool showDialog:@"请输入正确的手机号" message:nil waitingTime:1.5];
    }else{
        [KJSRequest RegisterSendMessageWithPhoneNumber:self.phoneTF.text ret:^(BasicResult *model) {
            if ([model.messageresult isEqualToString:@"0"]) {
                self.registerBtn.enabled = YES;
                [self.registerBtn setBackgroundColor:[UIColor kjsMainColor]];
                [GUATool showDialog:@"验证码已发送" message:nil waitingTime:1.5];
                self.countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
            }
        }];
    }
    
}

-(void)timeFireMethod{
    
    self.secondsCountDown--;
    self.getCodeBtn.titleLabel.text = [NSString stringWithFormat:@"%d秒",self.secondsCountDown];
    [self.getCodeBtn setTitle:[NSString stringWithFormat:@"%d秒",self.secondsCountDown] forState:UIControlStateNormal];
    self.getCodeBtn.enabled = NO;
    if (self.secondsCountDown == 0) {
        [self.countDownTimer invalidate];
        [self.getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.getCodeBtn.enabled = YES;
        self.secondsCountDown = CodeTimeInterval;
    }
    
    
}
@end
